﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace KLTP2UI
{
	internal class AI
	{
		internal class ThreadRenamePendingEventArgs: EventArgs
		{
			public int ThreadIdx { get; private set; }
			public string Name { get; private set; }

			public ThreadRenamePendingEventArgs (int threadIdx, string suggestedName)
			{
				ThreadIdx = threadIdx;
				Name = suggestedName;
			}
		}

		public event EventHandler<ThreadRenamePendingEventArgs> ThreadRenamePending;
		private bool _isRunning;
		private Thread _aiThread;

		private void FireThreadRenamePending(ThreadRenamePendingEventArgs e)
		{
			var handler = ThreadRenamePending;
			if (handler != null) 
				handler(this, e);
		}

		public void Run()
		{
			if(_isRunning)
				return;

			_aiThread = new Thread(Worker) {IsBackground = true, Name = "AI thread"};
			_aiThread.Start();
		}

		public void Stop()
		{
			_isRunning = false;
			if(DllMem.IsThreadJoinable(_aiThread))
				_aiThread.Join();
		}

		private void Worker()
		{
			_isRunning = true;
			
			Thread.Sleep(5000);

			while (_isRunning)
			{


				FireThreadRenamePending(new ThreadRenamePendingEventArgs(0, "Test!"));
				_isRunning = false;
			}
		}
	}
}
